import Util from '@helper/util';
import store from '../../store';

/**
 * 检查用户登录授权状态
 */
export default (to, from, next) => {
	const user = store.getters['User/getUserInfo'];
	if (!user && to.meta.requireAuth === true) {
		const token = Util.getCookie('token');
		if (token) {
			store.dispatch('User/checkAuth', {
				success: () => next(),
				exception: () => {
					Util.deleteCookie('token');
					next('login');
				},
			});
		} else {
			next('login');
		}
	} else {
		next();
	}
};
